/*  
     PlAr is Platform Arena: a 2D multiplayer shooting game
     Copyright (C) 2009 Ragagnin Antonio <spocchio@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package plar.core.defaultElements;

import java.awt.Point;
import java.util.ArrayList;

import plar.core.Action;
import plar.core.Common;
import plar.core.Element;
import plar.core.Parameters;
import plar.core.Sprite;
import plar.core.defaultActions.Move;

public class Flash extends Element {
	ArrayList <Sprite> spriteSet;
	
	public Flash() {
		super();
		spriteSet = new ArrayList <Sprite>();
		setProperty("type", "flash");
		setProperty("isRunnable", true);
		setProperty("canCollide", false);
		setProperty("spriteSet", spriteSet);
		actions.addAction("clock", new Clock());
		//Common.info("static prite generated!");
	}

	
	public ArrayList <Sprite> getSpriteSet()
	{
		return spriteSet;
	}
	
	public Sprite getSprite(int i)
	{
		return spriteSet.get(i);
	}
	
	
	public void addStaticSprite(String filename, int life)
	{
		spriteSet.add(new Sprite(filename,life));
		
	}
	
	public void addSprite(Sprite s)
	{
		spriteSet.add(s);
		
	}
	
	public void Initialize()
	{
		setElementSprite(0);
	}
	@Override
	public void run() {

		super.run();

	}
	
	public void setElementSprite(int i)
		{
			ArrayList <Sprite> al =new ArrayList <Sprite> ();
			al.add(spriteSet.get(i));
		 super.setProperty("currentSpriteSet",al);	
		}
	class Clock implements Action {
		int index=0;
		int time=0;
		public Clock() {

		}

		
		public Parameters call(Parameters parameters)

		{
			
			Flash me = (Flash) parameters.get("me");
			
			
			String subAction = (String) parameters.get("subAction");
			//Common.info("static prite "+subAction);
			if (subAction == "main") {
	
				time++;
				if(time>me.getSprite(index).time)
				{
						if(index>=me.getSpriteSet().size()-1)
						{
							Common.info("static prite morte con onore:"+getSpriteSet().size());
							parameters.put("subAction", "new");
							parameters.put("me", me);
							parameters.put("level", level);
							me.actions.runAction("destroy", parameters);
						}
						else
						{
							index++;
							time =	0;
							//me.setProperty("currentSpriteSet",spriteSet.get(index).filename);
							Common.info("index++="+index);
							me.setElementSprite(index);
						}
				}else
				{
					time++;
				}
			
			}
			// Common.info(getCurrentSpriteSet().toString());
			return null;
		}
	}
	
}
